/*
 * createAccountWindow.java
 *
 * Created on 11. Dezember 2003, 19:10
 */

/**
 *
 * @author  ChrM (chrmac@gmx.de)
 */

package info.chrm.jtalker;

import info.chrm.helper.*;

public class CreateChatroomWindow extends javax.swing.JDialog implements JTalkerClientCommandInterface {
    JTalkerClientFront parent = null;
    
    /** Creates new form createAccountWindow */
    public CreateChatroomWindow(JTalkerClientFront _parent, boolean _modal) {
        super(_parent, _modal);
	this.parent = _parent;
        this.initComponents();
        this.getRootPane().setDefaultButton(this.jButton1); 
	this.setLocationRelativeTo(_parent);
	this.setResizable(false);
	this.setTitle("Create a Chatroom");
        this.parent.theClient.registerForCommandResponse(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jUsersField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        ClassLoader cl = ResourceAnchor.class.getClassLoader();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

	jLabel1.setIcon(new javax.swing.ImageIcon(cl.getResource("icons/new_chatroom.jpg")));
        jLabel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel1.add(jLabel1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setLayout(null);

        jPanel2.setPreferredSize(new java.awt.Dimension(300, 195));
        jLabel2.setFont(new java.awt.Font("Dialog", 1, 18));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Create Chatroom");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(40, 10, 220, 25);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Room Name:");
        jPanel2.add(jLabel3);
        jLabel3.setBounds(10, 60, 110, 18);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Users:");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(10, 90, 110, 18);

        jTextField1.setToolTipText("Name for the new chatroom");
	jTextField1.setText(this.parent.theClient.userName + "-room");
        jPanel2.add(jTextField1);
        jTextField1.setBounds(130, 60, 150, 22);

        jUsersField1.setToolTipText("Allowed users, separated by ','");
	jUsersField1.setText(this.parent.theClient.userName + ",");
        jPanel2.add(jUsersField1);
        jUsersField1.setBounds(130, 90, 150, 22);

        jButton1.setText("Create");
        jButton1.setToolTipText("Creates a chatroom");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel2.add(jButton1);
        jButton1.setBounds(190, 130, 88, 28);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel2.add(jLabel5);
        jLabel5.setBounds(10, 170, 270, 20);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Create Account
        this.jLabel5.setText("Checking inputs...");
        String new_room_name = this.jTextField1.getText();
        String new_users = this.jUsersField1.getText();
        
        if(new_room_name.length() < 5 ) {
            this.jLabel5.setText("Name to short!");
            return;
        }

	if(!CreateChatroomWindow.isAllowedUserName(new_room_name)) {
            this.jLabel5.setText("Forbidden characters in name!");
            return;
	}
                
        // Call chatroom creation procedure
	if(this.parent.isConnected) {
            this.parent.theClient.sendMessage("std::createChatRoom(\"" + new_room_name + "\", \"" + new_users + "\")", null, "command", null, "usercommand11");
        }
	
        //this.closeDialog(null);
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
   
    /** Static method for checking for allowed user names 
     *  @param _input Input String for checking 
     *  @return True, if the input is an allowed user name */    
    public static boolean isAllowedUserName(String _input) {
        if(_input.length() > 20)
            return false;

	return _input.matches("[A-Za-z0-9-]+");
    }

    /** A command answer will be send to us from the server using this method 
     *  @return true, if the _id belongs to our command */
    public boolean setCommandAnswer(String _id, String _from, String _content) {
        if(_id.equals("usercommand11")) {
            // TODO check the answer (error/info/...)
            this.jLabel5.setText(_content);
        }

        return true;
    }

    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        new CreateChatroomWindow(new javax.swing.JFrame(), true).show();
    }*/
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jUsersField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    
}
